package thread;

public class SyncDemo2
{
    public static void main(String[] args)
    {
        Shop shop =  new Shop();
        Shop shop1 = new Shop();
        Thread t1 = new Thread("zz")
        {
            @Override
            public void run()
            {
                shop.buy();
            }
        };
        Thread t2 = new Thread("cc")
        {
            @Override
            public void run()
            {
                shop1.buy();
            }
        };
        Thread t3 = new Thread("aa")
        {
            @Override
            public void run()
            {
                shop.buy();
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}


class Shop
{
    public void buy()
    {
        try
        {
            Thread t1 = Thread.currentThread();

            System.out.println(t1.getName() + " is selecting clothes");

            Thread.sleep(2000);
            synchronized (this)
            {
                System.out.println(t1.getName() + " is trying on clothes");
                Thread.sleep(2000);
            }


            System.out.println(t1.getName() + " is buying");
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
